package org.aptech.hdax.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.aptech.hdax.dao.SysRoleDao;
import org.aptech.hdax.dao.SysUserDao;
import org.aptech.hdax.pojo.SysRole;
import org.aptech.hdax.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sysUser")
public class SysUserController {
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysRoleDao sysRoleDao;

//    @ExceptionHandler
//    public String yichangle(RuntimeException e) throws Exception{
//        return "hello";
//    }

    @RequestMapping("/index")
    public String index(ModelMap modelMap) throws Exception{
        modelMap.put("roleList",sysRoleDao.getAll());
        return "system/user/index";
    }

    @RequestMapping("/form")
    public String form() throws Exception {
        return "system/user/form";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
//    @PostMapping("/add")
    public Map<String,Object> add(SysUser sysUser,String roleIds) throws Exception{
        Map<String,Object> map = new HashMap<>();

        String[] array = roleIds.split(",");
        Integer[] ids = new Integer[array.length];
        for (int i = 0; i < array.length; i++) {
            ids[i] = Integer.parseInt(array[i]);
        }

        //对密码进行加密，放入数据库
        /*Md5Hash md5Hash = new Md5Hash(sysUser.getPassword(),sysUser.getSalit());
        sysUser.setPassword(md5Hash.toString());*/

        sysUserDao.add(sysUser);
        sysUserDao.addUserRole(sysUser.getId(),ids);

        return map;
    }

    @PostMapping("/edit")
    @ResponseBody
    public Map<String,Object> edit(SysUser sysUser,String roleIds) throws Exception{
        Map<String,Object> map=new HashMap<>();
        String[] array=roleIds.split(",");
        Integer[] ids=new Integer[array.length];
        for (int i=0;i<array.length;i++){
            ids[i] =Integer.parseInt(array[i]);
        }
        sysUserDao.update(sysUser);
        sysUserDao.deleteUserRole(sysUser.getId());
        sysUserDao.addUserRole(sysUser.getId(),ids);
        return map;
    }

    @RequestMapping("/view")
    @ResponseBody
    public SysUser view(Integer id) throws Exception{
        return sysUserDao.get(id);
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer limit, SysUser condition,String roleIds) throws Exception{
        Map<String,Object> map = new HashMap<>();

        //将角色编号转换到角色集合中
        if (roleIds != null && !roleIds.equals("") && roleIds.length()!=0){
            String[] array = roleIds.split(",");
            List<SysRole> roles = new ArrayList<>();
            for (int i = 0; i < array.length; i++) {
                SysRole role = new SysRole();
                role.setId(Integer.parseInt(array[i]));
                roles.add(role);
            }
            condition.setRoles(roles);
        }
        Page<SysUser> pager = PageHelper.startPage(page,limit);
        List<SysUser> list = sysUserDao.getList(condition);

        map.put("count", pager.getTotal());
        map.put("data",list);
        map.put("code",0);
        map.put("msg",null);

        return map;
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public Map<String,Object> batchDelete(Integer[] ids) throws Exception{
        Map<String,Object> map = new HashMap<>();
        sysUserDao.batchDelete(ids);
        return map;
    }

}
